<?php
/**
 * 技师服务层
 */
namespace app\service;
use think\Db;
class TechnService
{
	/**
	 * 技师申请类型
	 */
	public static $type_apply_array = [
		1 => [
			'value' => 1,
			'name' => '入驻申请',
			'steps' => 2
		],
		2 => [
			'value' => 2,
			'name' => '资料修改申请',
			'steps' => 1
		]
	];

	/**
	 * 性别
	 */
	public static $sex_array = [
		0 => ['value' => 0, 'name' => '未知'],
		1 => ['value' => 1, 'name' => '男'],
		2 => ['value' => 2, 'name' => '女']
	];

	/**
	 * 技师申请最终状态
	 */
	public static $status_over_apply_array = [
		0 => ['value' => 0, 'name' => '待审核'],
		1 => ['value' => 1, 'name' => '进行中'],
		2 => ['value' => 2, 'name' => '已通过'],
		3 => ['value' => 3, 'name' => '已拒绝']
	];


	/**
	 * 获取技师可预约时间
	 */
	public static function TechnPreTime($params = [])
	{
		//当前时间
        $current_time = time();
        //预约间隔时间(15分钟)
        $interval_time = $current_time+900;
        //最终预约时间
        $final_time = $interval_time;
        //技师预约时间节点
        $reservation_node = [];
        //如果技师id存在查询技师
        $techn_info = [];
        if(!empty($params['techn_id']))
        {
        	$techn_info = Db::name('techn')->where(['id' => $params['techn_id']])->find();
        	if(!empty($techn_info))
        	{
        		$techn_info['no_pre_hour_slot'] = !empty($techn_info['no_pre_hour_slot']) ? json_decode($techn_info['no_pre_hour_slot'], true) : [];
        		//查询技师已预约的订单
        		$order_list = Db::name('order')->where(['techn_id' => $techn_info['id'], 'status' => ['in', '0,1,2,3,4,5'], 'order_model' => 0])->select();
        		if(!empty($order_list))
        		{
        			foreach($order_list as $key => $value)
        			{

        				if(!empty($value['pre_start_time']) && !empty($value['pre_end_time']))
        				{

        					//处理时间节点
        					$pre_start_time = $value['pre_start_time'];
        					$pre_end_time = $value['pre_end_time']+900;
        					for($j = $pre_start_time; $j <= $pre_end_time; $j += 1800)
        					{

        					    if(date('i',$j)!='00'&&date('i',$j)!='30'){

        					        if(date('i',$j)>30){
        						        $reservation_node[] = date('Y-m-d H:00:00', $j+1800);
        					        }else{
        					            
        						        $reservation_node[] = date('Y-m-d H:30:00', $j);
        					        }
        					    }else{
        						    $reservation_node[] = date('Y-m-d H:i:00', $j);
        					    }
        				// 		$reservation_node[] = date('Y-m-d H:i:s', $j);
        					}
        				}
        			}
        		}
        	}
        }


        $list_data = [];
		$week_arr = ["周日","周一","周二","周三","周四","周五","周六"];
		//开始时间
		$start_time = strtotime(date('Y-m-d', time()));
		//结束时间
		$end_time = strtotime(date('Y-m-d', strtotime("+2 day")));
		//算出开始时间与结束时间中间间隔的时间
		$key = 0;
		for($i = $start_time; $i <= $end_time; $i += 86400)
		{
			$list_data[$key]['day'] = date('m-d', $i);
			$list_data[$key]['week'] = $week_arr[date("w", $i)];

			$time_slot = [];
			$slot_key = 0;
			//如果技师接单状态
			if(!empty($techn_info) && $techn_info['work_status'] == 1)
			{
				//开始时间段
				$start_slot = strtotime(date('Y-m-d', $i).' 00:00:00');
				//结束时间段
				$end_slot = strtotime(date('Y-m-d', $i).' 23:59:59');
				//判断是否选择接单开始时间与结束时间
				$work_start_hour = !empty($techn_info['work_start_hour']) ? strtotime(date('Y-m-d', $i).' '.$techn_info['work_start_hour'].':00') : strtotime(date('Y-m-d', $i).' 00:00:00');
				
				$work_end_hour = !empty($techn_info['work_end_hour']) ? strtotime(date('Y-m-d', $i).' '.$techn_info['work_end_hour'].':00') : strtotime(date('Y-m-d', $i).' 23:59:59');
				for($j = $start_slot; $j <= $end_slot; $j += 1800)
				{
					//如果大于当前时间才显示
					if($work_start_hour > $work_end_hour)
					{
						if($j >= $final_time && ($j >= $work_start_hour || $j <= $work_end_hour) && !in_array(date('Y-m-d H:i:s', $j), $techn_info['no_pre_hour_slot']) && !in_array(date('Y-m-d H:i:s', $j), $reservation_node))
						{
							$time_slot[$slot_key]['time'] = date('H:i', $j);
							$time_slot[$slot_key]['date'] = date('Y-m-d H:i:s', $j);
							$slot_key++;
						}
					}elseif ($work_start_hour < $work_end_hour) {
						if($j >= $final_time && $j >= $work_start_hour && $j <= $work_end_hour && !in_array(date('Y-m-d H:i:s', $j), $techn_info['no_pre_hour_slot']) && !in_array(date('Y-m-d H:i:s', $j), $reservation_node))
						{
							$time_slot[$slot_key]['time'] = date('H:i', $j);
							$time_slot[$slot_key]['date'] = date('Y-m-d H:i:s', $j);
							$slot_key++;
						}
					}else
					{
						if($j >= $final_time && !in_array(date('Y-m-d H:i:s', $j), $techn_info['no_pre_hour_slot']) && !in_array(date('Y-m-d H:i:s', $j), $reservation_node))
						{
							$time_slot[$slot_key]['time'] = date('H:i', $j);
							$time_slot[$slot_key]['date'] = date('Y-m-d H:i:s', $j);
							$slot_key++;
						}
					}
				}
			}
			$list_data[$key]['time_slot'] = $time_slot;
			$key++;
		}
		return $list_data;
	}
	
	//可预约时间更改
	public static function TechnPreTimey($techn_id){
        
		$pre_time = '';
        $techn_pre_time=self::TechnPreTime(['techn_id' => $techn_id]);
        if(!empty($techn_pre_time))
		{
			$pre_time_array = array_merge_recursive($techn_pre_time[0]['time_slot'], $techn_pre_time[1]['time_slot'], $techn_pre_time[2]['time_slot']);

            if(!empty($pre_time_array))
			{
				$pre_time = $pre_time_array[0]['date'];
			}
		}
		if(!empty($pre_time)){
		    $pre_time=strtotime($pre_time);
		}

		db::name('techn')->where('id',$techn_id)->update(['pre_time_y'=>$pre_time]);
    }
	
	
}